﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.ModuleEdits;
using System.Linq;

namespace ECRMS.WinForm.Modules 
{
    [ModuleInstance((int)ModuleEnum.报表分配)]
    public partial class ReportPlanModule :BaseModule
    {
        public ReportPlanModule()
        {
            InitializeComponent();
            base.core.CreateWaitDialog("初始化中请稍候", "报表设计");
            InitView();
            InitData();
            InitEventHandler();
            core.CloseWaitDialog();
        }

        private void InitEventHandler()
        {
            //语言切换
            base.core.LangChanged += new Core.LanguageChangedHandler(this.ChangeLanguage);
        }

        //true 为不需要去服务端重新获取数据
        private void InitData(bool isPostBack = false)
        {
            if (!isPostBack)
            {
                FuncResult result = core.CoreReportOfPlan.GetReports(core.CoreUser.UserInfo.CorporationRefId);
                if (result.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                    return;
                }
            }
            this.gdReport.DataSource = core.CoreReportOfPlan.Reports;
            this.gdReport.RefreshDataSource();
        }

        private void InitView()
        {
            base.selectBarButtonVisiable = false;
            base.saveBarButtonVisiable = false;
            base.otherBarButton3Visiable = false;
            base.outBarButtonVisiable = false;

            base.other1BarButton1ImgaeIndex = 44;
            base.other2BarButton2ImgaeIndex = 45;

            base.other1BarButton1Content = "上移";
            base.other2BarButton2Content = "下移";
        }
        //新增
        public override void newBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.newBarButton_ItemClick(sender, e);

            core.CoreReportOfPlan.OperatorRow = new CtReport();
            core.CoreReportOfPlan.OperatorRow.CorporationRefId = core.CoreUser.UserInfo.CorporationRefId;
            core.CoreReportOfPlan.OperatorRow.ReportPartRefId = Guid.NewGuid();
            core.CoreReportOfPlan.OperatorRow.SeqId = core.CoreReportOfPlan.Reports.Count() + 1;

            ReportEdit reportEdit = new ReportEdit(this.RectangleToScreen(Bounds), OperationEnum.New);
            DialogResult result = reportEdit.ShowDialog();

            if (result == DialogResult.OK)
            {
                //数据量不大 所以去服务端获取一遍
                InitData();
            }

            core.CoreReportOfPlan.OperatorRow = null;
            reportEdit.Dispose();

        }

        //修改
        public override void editBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.editBarButton_ItemClick(sender, e);
            Guid rowIndex=core.CoreCommon.GetGridFocusRowRefId(gdReport, gridView1, "ReportPartRefId");
            if (rowIndex == Guid.Empty)
            {
                return;
            }
            CtReport report = core.CoreReportOfPlan.Reports.FirstOrDefault(p => p.ReportPartRefId == rowIndex);

            core.CoreReportOfPlan.OperatorRow = report;

            ReportEdit reportEdit = new ReportEdit(this.RectangleToScreen(Bounds), OperationEnum.Modify);
            DialogResult result = reportEdit.ShowDialog();

            if (result == DialogResult.OK)
            {
                //数据量不大 所以去服务端获取一遍
                InitData();
            }

            core.CoreReportOfPlan.OperatorRow = null;
            reportEdit.Dispose();

        }

        public override void delBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.delBarButton_ItemClick(sender, e);
            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdReport, gridView1, "ReportPartRefId");
            if (rowIndex == Guid.Empty)
            {
                return;
            }
            CtReport report = core.CoreReportOfPlan.Reports.FirstOrDefault(p => p.ReportPartRefId == rowIndex);
            core.CoreReportOfPlan.OperatorRow = report;

            DialogResult result = XtraMessageBox.Show(this,"确定删除此报表模块？","删除提醒",MessageBoxButtons.YesNo);

            if (result == DialogResult.No)
            {
                core.CoreReportOfPlan.OperatorRow = null;
                return;
            }
            else
            {
                core.CoreReportOfPlan.OperatorRow.IsDisabled = true;
                FuncResult result1= core.CoreReportOfPlan.UpdateReportNoContent();
                if (result1.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result1.Message));
                    core.CoreReportOfPlan.OperatorRow = null;
                    return;
                }
                //数据量不大 所以去服务端获取一遍
                InitData();
            }
        }
        //上移
        public override void otherBarButton1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            core.CreateWaitDialog("数据提交中，请稍后", "数据提交中", 150, 50, true, this);
            base.otherBarButton1_ItemClick(sender, e);
            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdReport, gridView1, "ReportPartRefId");
            if (rowIndex == Guid.Empty)
            {
                core.CloseWaitDialog();
                return;
            }
            //准备上移的模块
            CtReport report1 = core.CoreReportOfPlan.Reports.FirstOrDefault(p => p.ReportPartRefId == rowIndex);
            if (report1.SeqId == 1)
            {
                core.CloseWaitDialog();
                return;
            }
            //需要下移的模块
            CtReport report2 = core.CoreReportOfPlan.Reports.FirstOrDefault(p => p.SeqId == report1.SeqId - 1);

            report1.SeqId--;
            report2.SeqId++;
            List<CtReport> reports=new List<CtReport>();
            reports.Add(new CtReport { ReportPartRefId=report1.ReportPartRefId,SeqId=report1.SeqId});
            reports.Add(new CtReport { ReportPartRefId = report2.ReportPartRefId, SeqId = report2.SeqId });
            FuncResult result = core.CoreReportOfPlan.UpdateReportSeq(reports);
            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }
            //数据量不大 所以去服务端获取一遍
            InitData();
            core.CloseWaitDialog();
        }

        public override void otherBarButton2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            core.CreateWaitDialog("数据提交中，请稍后", "数据提交中", 150, 50, true, this);
            base.otherBarButton2_ItemClick(sender, e);
            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdReport, gridView1, "ReportPartRefId");
            if (rowIndex == Guid.Empty)
            {
                core.CloseWaitDialog();
                return;
            }
            //准备下移的模块
            CtReport report1 = core.CoreReportOfPlan.Reports.FirstOrDefault(p => p.ReportPartRefId == rowIndex);
            //需要上移的模块
            CtReport report2 = core.CoreReportOfPlan.Reports.FirstOrDefault(p => p.SeqId == report1.SeqId + 1);
            if (report2 == null)//是最下面一行了
            {
                core.CloseWaitDialog();
                return;
            }

            report1.SeqId++;
            report2.SeqId--;
            List<CtReport> reports = new List<CtReport>();
            reports.Add(new CtReport { ReportPartRefId = report1.ReportPartRefId, SeqId = report1.SeqId });
            reports.Add(new CtReport { ReportPartRefId = report2.ReportPartRefId, SeqId = report2.SeqId });
            FuncResult result = core.CoreReportOfPlan.UpdateReportSeq(reports);
         
            if (result.Status == 0)
            {
                core.CloseWaitDialog();
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }
            //数据量不大 所以去服务端获取一遍
            InitData();
            core.CloseWaitDialog();
        }

        private void ChangeLanguage(LangEnum lang)
        {
            //switch (core.NowLanguage)
            switch (lang)
            {
                case LangEnum.China:
                case LangEnum.中文:

                    break;
                case LangEnum.English:
                case LangEnum.英文:

                    break;
                default:
                    break;
            }
        }
    }
}
